<template>
  <basic-container>

    <avue-crud :option="option"
               :table-loading="loading"
               :data="data"
               ref="crud"
               v-model="form"
               :page="page"
               :permission="permissionList"
               @row-del="rowDel"
               @row-update="rowUpdate"
               @row-save="rowSave"
               @search-change="searchChange"
               @search-reset="searchReset"
               @selection-change="selectionChange"
               @current-change="currentChange"
               @size-change="sizeChange"
               @on-load="onLoad"
               v-if="showMain"
    >
<!--      <template slot="menuTop">-->
<!--        <el-button type="danger"-->
<!--                   size="small"-->
<!--                   icon="el-icon-delete"-->
<!--                   plain-->
<!--                   @click="handleDelete">删 除-->
<!--        </el-button>-->
<!--      </template>-->
      <template slot-scope="{type,size,row,index}" slot="menu">
        <el-button icon="el-icon-tickets" :size="size" :type="type" v-if="row.isPj" @click="evaluateClick(row)">评价
        </el-button>
        <el-button icon="el-icon-tickets" :size="size" :type="type" @click="showView(row)">
          历史评价
        </el-button>
      </template>
    </avue-crud>


    <el-dialog title="评价"
               append-to-body
               :visible.sync="excelBox"
               width="600px">
      <avue-form :option="excelOption" v-model="excelForm">
      </avue-form>
      <span slot="footer" class="dialog-footer">
          <el-button type="primary" @click="submit">确 定</el-button>
          <el-button @click="excelBox = false">取 消</el-button>
        </span>
    </el-dialog>
    <yxHistory v-bind:csId="csId" :showMainView="showMainView" v-if="(!showMain)&&show" ref="eps1"></yxHistory>
  </basic-container>
</template>

<script>
import {getList, remove, update, add, addEvaluate, getIsEvaluatedByTeacher} from "@/api/service/yx";
import {mapGetters} from "vuex";
import yxHistory from "@/views/service/yxHistory.vue";

export default {
  components: {
    yxHistory
  },
  data() {
    return {
      showMain: true,
      show: false,
      csId:'',
      form: {},
      selectionList: [],
      query: {njId: '0',types:1},
      loading: true,
      excelBox: false,
      excelForm: {},
      selectData: {},
      excelOption: {
        submitBtn: false,
        emptyBtn: false,
        column: [
          {
            label: "评价标签",
            prop: "dictId",
            type: "checkbox",
            span: 24,
            minRows: 6,
            rules: [{
              required: true,
              message: "请选择评价标签",
              trigger: "blur"
            }],
            dicUrl: "/api/ManageEvaluated/GetEvaluatedListByType?types=2",
            props: {
              label: "dictName",
              value: "dictId"
            }
          },
          {
            label: "",
            prop: "typeValue",
            // span: 24,
            // disabled: true,
            type: "textarea",
            span: 24,
            minRows: 6,
            placeholder:'请输入其他印象',
            rules: [{
              required: false,
              message: "请输入其他印象",
              trigger: "blur"
            }]
          },
        ]
      },
      page: {
        pageSize: 10,
        currentPage: 1,
        total: 0
      },
      option: {
        height: 'auto',
        calcHeight: 210,
        searchShow: true,
        searchMenuSpan: 6,
        tip: false,
        border: true,
        index: true,
        selection: true,
        viewBtn: true,
        align: 'center',
        menuAlign: 'center',
        column: [
          {
            label: "年级",
            prop: "njId",
            search: true,
            span: 24,
            rules: [{
              required: true,
              message: "请选择年级",
              trigger: "blur"
            }],
            cascader: ['bjId'],
            type: "tree",
            dicUrl: "/api/ManageEvaluated/GetTeacherGrade",
            props: {
              label: "name",
              value: "id"
            },
          },
          {
            label: "班级",
            prop: "bjId",
            search: true,
            addDisplay: false,
            editDisplay: false,
            viewDisplay: false,
            hide:true,
            span: 24,
            rules: [{
              required: true,
              message: "请选择班级",
              trigger: "blur"
            }],
            type: "tree",
            dicUrl: "/api/ManageEvaluated/GetTeacherClass?njId={{key}}",
            props: {
              label: "name",
              value: "id"
            },
          },
          {
            label: "班级",
            prop: "bnName",
            span: 24,
            rules: [{
              required: false,
              message: "请输入班级",
              trigger: "blur"
            }]
          },
          {
            label: "学生姓名",
            prop: "xsName",
            span: 24,
            // search: true,
            search: {
              defaultValue: "默认值" ,// 设置搜索条件的默认值
              value:'默认值',
            },
            rules: [{
              required: false,
              message: "请输入学生姓名",
              trigger: "blur"
            }],
          },

          // {
          //   label: "租户ID",
          //   prop: "tenantId",
          //   search: true,
          //   addDisplay: false,
          //   editDisplay: false,
          //   span: 24,
          //   rules: [{
          //     required: true,
          //     message: "请输入租户ID",
          //     trigger: "blur"
          //   }]
          // },
          // {
          //   label: "租户名称",
          //   prop: "tenantName",
          //   search: true,
          //   span: 24,
          //   rules: [{
          //     required: true,
          //     message: "请输入参数名称",
          //     trigger: "blur"
          //   }]
          // },
          {
            label: "今日是否评价",
            prop: "isPj",
            span: 24,
            rules: [{
              required: true,
              message: "请输入联系人",
              trigger: "blur"
            }],
            type: "select",
            dicData: [
              {
                label: "已评价",
                value: false
              },
              {
                label: "未评价",
                value: true
              },
            ],
          },
        ]
      },
      data: []
    };
  },
  computed: {
    ...mapGetters(["permission"]),
    permissionList() {
      return {
        addBtn: false,//this.vaildData(this.permission.tenant_add, false),
        viewBtn: false,//this.vaildData(this.permission.tenant_view, false),
        delBtn: false,//this.vaildData(this.permission.tenant_delete, false),
        editBtn: false,//this.vaildData(this.permission.tenant_edit, false)
      };
    },
    ids() {
      let ids = [];
      this.selectionList.forEach(ele => {
        ids.push(ele.id);
      });
      return ids.join(",");
    }
  },
  methods: {
    //打开评价弹窗
    evaluateClick(row) {
      // dictId typeValue
      this.excelForm.dictId = []
      this.excelForm.typeValue = ''
      let params = {
        appUserIdBe: row.id,
        // dictId: this.excelForm.dictId,
        // typeValue: this.excelForm.typeValue
      }
      getIsEvaluatedByTeacher(params).then((res) => {
        if (res.data.data){
          this.selectData = row
          this.excelBox = true
        }else {
          this.$message({
            type: "warning",
            message: "已评价过，不可再次评价!"
          });
          this.onLoad(this.page);
        }
      }, error => {
        window.console.log(error);
      });
    },
    // 弹窗确定
    submit() {
      let params = {
        appUserIdBe: this.selectData.id,
        dictId: this.excelForm.dictId,
        typeValue: this.excelForm.typeValue
      }
      addEvaluate(params).then(() => {
        this.$message({
          type: "success",
          message: "操作成功!"
        });
        this.onLoad(this.page);
        this.excelBox = false
      }, error => {
        window.console.log(error);
      });
    },
    rowSave(row, done, loading) {
      add(row).then(() => {
        done();
        this.onLoad(this.page);
        this.$message({
          type: "success",
          message: "操作成功!"
        });
      }, error => {
        window.console.log(error);
        loading();
      });
    },
    rowUpdate(row, index, done, loading) {
      update(row).then(() => {
        done();
        this.onLoad(this.page);
        this.$message({
          type: "success",
          message: "操作成功!"
        });
      }, error => {
        window.console.log(error);
        loading();
      });
    },
    rowDel(row) {
      this.$confirm("确定将选择数据删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          return remove({ids: row.id});
        })
        .then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
        });
    },
    searchReset() {
      this.query = {njId: '0',types:1};
      this.onLoad(this.page);
    },
    searchChange(params, done) {
      this.query = params;
      this.page.currentPage = 1;
      this.onLoad(this.page, params);
      done();
    },
    selectionChange(list) {
      this.selectionList = list;
    },
    handleDelete() {
      if (this.selectionList.length === 0) {
        this.$message.warning("请选择至少一条数据");
        return;
      }
      this.$confirm("确定将选择数据删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          return remove({ids: this.ids});
        })
        .then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
          this.$refs.crud.toggleSelection();
        });
    },
    currentChange(currentPage) {
      this.page.currentPage = currentPage;
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
    },
    onLoad(page, params = {}) {
      this.loading = true;
      getList(page.currentPage, page.pageSize, Object.assign(params, this.query)).then(res => {
        const data = res.data.data;
        this.page.total = data.total;
        this.data = data.records;
        this.loading = false;
      });
    },
    showView(row) {
      this.csId = row.id;
      //this.$refs.eps.onLoad({currentPage:1,pageSize:10});
      this.showMain = false;
      this.show = true;
    },
    showMainView() {
      this.showMain = true;
      this.show = false;
    },
  }
};
</script>

<style>
</style>
